package org.jeecg.modules.fk.service;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import org.jeecg.modules.fk.mapper.manual.model.kanban.FxgkRespDTO;
import org.jeecg.modules.fk.mapper.manual.model.kanban.QyfxRespDTO;
import org.jeecg.modules.fk.mapper.manual.model.kanban.RegionHysflRespDTO;
import org.jeecg.modules.fk.mapper.manual.model.kanban.RiskInfoRespDTO;
import org.jeecg.modules.fk.constant.ZbTypeEnum;
import org.jeecg.modules.fk.vo.req.kanban.QyfxDto;
import org.jeecg.modules.fk.vo.req.kanban.RegionHysflDto;
import org.jeecg.modules.fk.vo.req.kanban.RiskInfoDto;
import org.jeecg.modules.fk.repository.FkDaoRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class KanbanService {

    @Autowired
    private FkDaoRepository daoRepository;

    public List<RegionHysflRespDTO> getRegionHysfl(RegionHysflDto req) {
        short year = (short) (Calendar.getInstance().get(Calendar.YEAR) - 1);//取上一年

        // 暂时固定取2019年度的行业税负率
        year = 2019;

        return this.daoRepository.getRiskStandardZzsHySflExtMapper().getRegionHysfl(year, req.getRegion(), req.getHyDms(), req.getZcdzxzqhszDm());
    }

    public QyfxRespDTO getQyfx(QyfxDto req) {
        QyfxRespDTO qyfx = new QyfxRespDTO();

//        SkssqVo skssqVo = new SkssqVo().invoke();
//        String skssqq = skssqVo.getSkssqq();
//        String skssqz = skssqVo.getSkssqz();
//        String qysdsQ = skssqVo.getQysdsQ();
//        String qysdsZ = skssqVo.getQysdsZ();

        LocalDate today = LocalDate.now();
        LocalDate lastMonthDate = today.minusMonths(1L);
        LocalDate firstDayOfLastMonth = lastMonthDate.with(TemporalAdjusters.firstDayOfMonth());
        LocalDate lastDayOfLastMonth = lastMonthDate.with(TemporalAdjusters.lastDayOfMonth());

        LocalDate lastYearDate = today.minusYears(1L);
        LocalDate firstDayOfLastYear = lastYearDate.with(TemporalAdjusters.firstDayOfYear());
        LocalDate lastDayOfLastYear = lastYearDate.with(TemporalAdjusters.lastDayOfYear());

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String skssqq = firstDayOfLastMonth.format(dateTimeFormatter);
        String skssqz = lastDayOfLastMonth.format(dateTimeFormatter);
        String qysdsQ = firstDayOfLastYear.format(dateTimeFormatter);
        String qysdsZ = lastDayOfLastYear.format(dateTimeFormatter);

        //高风险
        int gfxqysl = this.daoRepository.getRiskReportLevelExtMapper().getGfxqysl(skssqq, skssqz, qysdsQ, qysdsZ, req.getNsrsbhList());
        qyfx.setGfxqysl(gfxqysl);

        //中风险
        int zfxqysl = this.daoRepository.getRiskReportLevelExtMapper().getZfxqysl(skssqq, skssqz, qysdsQ, qysdsZ, req.getNsrsbhList());
        qyfx.setZfxqysl(zfxqysl);

        //低风险
        int dfxqysl = this.daoRepository.getRiskReportLevelExtMapper().getDfxqysl(skssqq, skssqz, qysdsQ, qysdsZ, req.getNsrsbhList());
        qyfx.setDfxqysl(dfxqysl);

        //无风险
        int wfxqysl = this.daoRepository.getRiskReportLevelExtMapper().getWfxqysl(skssqq, skssqz, qysdsQ, qysdsZ, req.getNsrsbhList());
        qyfx.setWfxqysl(wfxqysl);

        return qyfx;
    }

    public RiskInfoRespDTO getRiskInfo(RiskInfoDto req) {
        RiskInfoRespDTO result = new RiskInfoRespDTO();

//        SkssqVo skssqVo = new SkssqVo().invoke();
//        String skssqq = skssqVo.getSkssqq();
//        String skssqz = skssqVo.getSkssqz();
//        String qysdsQ = skssqVo.getQysdsQ();
//        String qysdsZ = skssqVo.getQysdsZ();

        LocalDate today = LocalDate.now();
        LocalDate lastMonthDate = today.minusMonths(1L);
        LocalDate firstDayOfLastMonth = lastMonthDate.with(TemporalAdjusters.firstDayOfMonth());
        LocalDate lastDayOfLastMonth = lastMonthDate.with(TemporalAdjusters.lastDayOfMonth());

        LocalDate lastYearDate = today.minusYears(1L);
        LocalDate firstDayOfLastYear = lastYearDate.with(TemporalAdjusters.firstDayOfYear());
        LocalDate lastDayOfLastYear = lastYearDate.with(TemporalAdjusters.lastDayOfYear());

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String skssqq = firstDayOfLastMonth.format(dateTimeFormatter);
        String skssqz = lastDayOfLastMonth.format(dateTimeFormatter);
        String qysdsQ = firstDayOfLastYear.format(dateTimeFormatter);
        String qysdsZ = lastDayOfLastYear.format(dateTimeFormatter);

        // 发票风险
        FxgkRespDTO fpfx = this.daoRepository.getRiskReportLevelExtMapper().getFxgk(skssqq, skssqz, req.getNsrsbhList(), ZbTypeEnum.ZZSFP.toString());
        if (null == fpfx) {
            fpfx = new FxgkRespDTO();
        }
        result.setFpfx(fpfx);

        // 增值税风险
        FxgkRespDTO zzsfx = this.daoRepository.getRiskReportLevelExtMapper().getFxgk(skssqq, skssqz, req.getNsrsbhList(), ZbTypeEnum.ZZS.toString());
        if (null == zzsfx) {
            zzsfx = new FxgkRespDTO();
        }
        result.setZzsfx(zzsfx);

        // 企业所得税风险
        FxgkRespDTO qysdsfx = this.daoRepository.getRiskReportLevelExtMapper().getFxgk(qysdsQ, qysdsZ, req.getNsrsbhList(), ZbTypeEnum.QYSDS.toString());
        if (null == qysdsfx) {
            qysdsfx = new FxgkRespDTO();
        }
        result.setQysdsfx(qysdsfx);

        return result;
    }

    private class SkssqVo {

        private String skssqq;
        private String skssqz;
        private String qysdsQ;
        private String qysdsZ;

        public String getSkssqq() {
            return skssqq;
        }

        public String getSkssqz() {
            return skssqz;
        }

        public String getQysdsQ() {
            return qysdsQ;
        }

        public String getQysdsZ() {
            return qysdsZ;
        }

        public SkssqVo invoke() {
            Date skssq = DateUtil.lastMonth();
            skssqq = DateUtil.format(DateUtil.beginOfMonth(skssq), DatePattern.NORM_DATE_PATTERN);
            skssqz = DateUtil.format(DateUtil.endOfMonth(skssq), DatePattern.NORM_DATE_PATTERN);

            String year = String.valueOf(Calendar.getInstance().get(Calendar.YEAR) - 1);
            qysdsQ = year + "-01-01";
            qysdsZ = year + "-12-31";
            return this;
        }

    }

}
